using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIObjects.TreeViewInterfaces;
#if SILVERLIGHT
using AbstraX.ClientInterfaces;
#else
using AbstraX.ServerInterfaces;
#endif
using Entities = AbstraX.BindingsTreeEntities;
using System.Collections.ObjectModel;
using AbstraX.Contracts;
using MvvmTreeView;
using System.Windows.Controls;
using System.ServiceModel.DomainServices.Client;

namespace AbstraX.ViewModels.BindingsTree
{
    public class BindingsTreeNodeTreeNodeReference : BindingsTreeNodeBase
    {
        private Entities.BindingsTreeNodeReference treeNodeReference;
        private ContextMenu contextMenu;

        public BindingsTreeNodeTreeNodeReference(IBindingsTreeService bindingsTreeService, ITreeView treeView, Entities.BindingsTreeNodeReference treeNodeReference) : base(bindingsTreeService, treeView)
        {
            this.treeNodeReference = treeNodeReference;
            this.Tag = treeNodeReference;
            this.DebugInfo = treeNodeReference.DebugInfo;
        }

        public override ContextMenu ContextMenu
        {
            get
            {
                if (contextMenu == null)
                {
                    contextMenu = new ContextMenu
                    {
                        Items = new List<IContextMenuItem>()
                        {
                            new ContextMenuItem
                            {
                                ID = "",
                                Text = "Break On Generator",
                                ClickHandler = (sender, e) =>
                                {
#if SILVERLIGHT
                                    bindingsTreeService.BreakOnGenerator(treeNodeReference.ID);
#endif
                                }
                            },
                            new ContextMenuItem
                            {
                                ID = "",
                                Text = "Break On Build",
                                ClickHandler = (sender, e) =>
                                {
#if SILVERLIGHT
                                    bindingsTreeService.BreakOnBuilder(treeNodeReference.ID);
#endif
                                }
                            }
                        }
                    };
                }

                return contextMenu;
            }
        }

        public override void LoadChildren()
        {
            var treeNodeReference = (Entities.BindingsTreeNodeReference)this.Tag;

#if SILVERLIGHT
            var children = bindingsTreeService.LoadChildren<Entities.BindingsTreeNodeReference>(treeNodeReference);

            children.CollectionChanged += (s, e) =>
            {
                ChildrenLoaded(children);
            }; 

#endif
            treeView.SetBusy(this, true);
        }

        public override void OnSelect()
        {
            this.TreeView.FireNodeSelected(this);
        }

        public override Entity Entity
        {
            get
            {
#if SILVERLIGHT
                return treeNodeReference;
#else
                return null;
#endif
            }
        }

        protected override void ChildrenLoaded(IBindingsTreeCollection children)
        {
            isChildrenLoaded = true;

            this.RemoveSpinster();

            base.AddChildren(children);

            treeView.SetBusy(this, false);
        }

        public override string Text
        {
            get
            {
                return treeNodeReference.GetName();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool HasLayoverImage<T>(out T image)
        {
            image = default(T);

#if SILVERLIGHT
            var bytes = bindingsTreeService.LoadShortcutImage();
            image = bindingsTreeService.ConvertImage<T>(bytes);
#endif 
            return true;
        }

        public override List<string> AddNewShortCuts
        {
            get { throw new NotImplementedException(); }
        }

        public override void AddNew(string shortCut)
        {
            throw new NotImplementedException();
        }

        public override void DragStarting(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e)
        {
        }

        public override void DragCompleted(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e)
        {
        }

        public override void ItemDropped(object sender, UIObjects.DragDropInterfaces.ItemDragEventArgs e, int insertionIndex)
        {
        }

        public override void GiveFeedback(object sender, UIObjects.DragDropInterfaces.GiveFeedbackEventArgs e)
        {
        }

        public override void DragEnter(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
        }

        public override void DragLeave(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
        }

        public override void DragOver(object sender, UIObjects.DragDropInterfaces.DragEventArgs e)
        {
        }

        public override float ChildOrdinal
        {
            get
            {
                return treeNodeReference.GetChildOrdinal();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool? HasChildren
        {
            get
            {
                return treeNodeReference.GetHasChildren();
            }
        }
    }
}
